package com.ldy.biz.shop.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.biz.ShopConst.OrderStatus;
import com.ldy.biz.shop.entity.Order;
import com.ldy.biz.shop.service.OrderService;
import com.ldy.core.common.Result;
import com.ldy.core.service.BaseOrgService;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 订单管理
 */
@RestController
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class OrderController {

	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderService orderService;
	
	@Autowired
	private BaseOrgService orgService;

	/**
	 * 列表分页查询
	 * 
	 * @param current
	 * @param size
	 * @param keyword
	 * @return
	 */
	@PostMapping("/finish/selectPage")
	public Result selectPageFinish(int current, int size, String keyword) {
		Page<Order> page = new Page<Order>(current, size);
		QueryWrapper<Order> query = new QueryWrapper<>();
		if (StringUtils.hasText(keyword)) {
			query.and(ct -> ct.like("order_phone", keyword).or().like("order_id", keyword));
		}
		// 查询和我有关系
		List<String> orgs = orgService.getMySubOrgs();
		if (CollectionUtil.isNotEmpty(orgs)) {
			query.in("org_code", orgs);
		}
		query.in("status", OrderStatus.已取消.name(), OrderStatus.已完成.name());
		query.orderByDesc("order_time");
		return Result.success(orderService.page(page, query));
	}
	

	/**
	 * 列表分页查询
	 * 
	 * @param current
	 * @param size
	 * @param keyword
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String keyword) {
		Page<Order> page = new Page<Order>(current, size);
		QueryWrapper<Order> query = new QueryWrapper<>();
		if (StringUtils.hasText(keyword)) {
			query.and(ct -> ct.like("order_phone", keyword).or().like("order_id", keyword));
		}
		// 查询和我有关系
		List<String> orgs = orgService.getMySubOrgs();
		if (CollectionUtil.isNotEmpty(orgs)) {
			query.in("org_code", orgs);
		}
		query.notIn("status", OrderStatus.已取消.name(), OrderStatus.已完成.name());
		query.orderByDesc("order_time");
		return Result.success(orderService.page(page, query));
	}


	/**
	 * 列表查询
	 * 
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		return Result.success(orderService.list());
	}
	
	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(Order param) {
		logger.info("method: {}, params:[app: {}]", "update", JSONObject.toJSONString(param));
		orderService.updateById(param);
		return Result.success();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		orderService.removeById(id);
		return Result.success();
	}
}
